#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Introduction to Programming in Java - An Interdisciplinary Approach

All book programs ported to Python by
Scott C. King (revisionx at {gee}ma1l <dot> c0m).

All program examples pass Pylint with a perfect 10.00 score.
Pylint:  http://www.pylint.org/


# Program 1.3.6 - Newton's method

Syntax:
python sqrt.py 2.0


Notes:
float(sys.argv[1]) == Double.parseDouble(args[0])

sys is a required import here in order to use command line arguments.
https://docs.python.org/2/tutorial/stdlib.html

math is a required import here for the exp() function for variable epsilon.
https://docs.python.org/2/library/math.html

'''

import sys
import math


def main():
    ''' Compute square root of number using Newton's method '''

    number = float(sys.argv[1])
    epsilon = math.exp(-15)
    estimate = number
    while abs(estimate - number/estimate) > epsilon * estimate:
        estimate = (number/estimate + estimate) / 2.0
    print estimate


if __name__ == '__main__':
    main()
